<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;


use cmf\controller\AdminBaseController;
use think\App;
use think\facade\Db;

class HxkjController extends AdminBaseController
{
    /**
     * @return void
     * 获取捆包号
     */
    public function confirm()
    {
        $type = input('type');

        if (!(is_numeric($type) && $type > 0)) {
            $this->error('非法参数');
        }

        $name = getname($type);

        $this->success('操作成功', 'index',$name);
//        $this->success('成功');
    }

    public function getplanname(){
        $type = input();
        $type = $type['type'];
        $log='';

        $typedd = Db::name('pp')
            ->column('tc', 'id');

//        var_dump($typedd[$type]);
        $log=$typedd[$type];
//        if($type==2){
//            $log='Q';
//        }elseif ($type==3){
//            $log='R';
//        }elseif ($type==4){
//            $log='H';
//        }
        $today = date('ymd', time());
//        $this->success('ok', '',$today);
        $newnum = Db::name('plan_num')->where('today', $today)->order('id desc')->find();
        if ($newnum) {
            Db::name('plan_num')->insertGetId(array('today' => $today, 'order_num' => $newnum['order_num'] + 1, 'addtime' => time(),
                'admin_id' => cmf_get_current_admin_id()));
            $dd = str_pad($newnum['order_num'] + 1, 3, '0', STR_PAD_LEFT);
            $name = $log . $today . $dd;
            $this->success('ok', '', $name);
        } else {
            Db::name('plan_num')->insertGetId(array('today' => $today, 'order_num' => 1, 'addtime' => time(),
                'admin_id' => cmf_get_current_admin_id()));
            $dd = str_pad(1, 3, '0', STR_PAD_LEFT);

            $name =$log . $today . $dd;
            $this->success('ok', '', $name);
        }
    }
    //获取零件号
    public function getpartno(){
        $coilarr=Db::name('partno')->where('delete_time', 0)->select();
        $data = array();
        $num = 0;
        // $steel_grade_arr_da = Db::name('steel_grade')
        // ->column('name', 'id');
       foreach ($coilarr as $k => $v) {
//   
           $data[$v['partno_id']]['value'] = $v['partno_id'];
     
           $data[$v['partno_id']]['text'] = $v['name'].'-'.$v['drawingno'];
           $data[$v['partno_id']]['d'] = $v['d'];
           $data[$v['partno_id']]['w'] = $v['w'];
           $data[$v['partno_id']]['h'] = $v['h'];
           $data[$v['partno_id']]['weight'] = $v['weight'];
           $data[$v['partno_id']]['grade_id'] = $v['grade_id'];
           $data[$v['partno_id']]['sheets'] = $v['sheets'];
       
       }
        $this->success("成功", '', $data);
    }
    public function getcoil(){
        $coilarr=Db::name('coil')->where('delete_time', 0)->select();
        $data = array();
        $num = 0;
        $steel_grade_arr_da = Db::name('steel_grade')
        ->column('name', 'id');
       foreach ($coilarr as $k => $v) {
//            var_dump($v);
           $data[$v['id']]['value'] = $v['id'];
           $st= isset($steel_grade_arr_da[$v['steel_grade_id']]) ? $steel_grade_arr_da[$v['steel_grade_id']] : '';
           //{$ao.name}-{$ao.coilno}-{$ao.product}-{$steel_grade_arr_da[$ao.steel_grade_id]}-{$ao.d}*{$ao.w}*{$ao.h}-{$ao.weight-$ao.occupied_we}吨
           $da=$v['weight']-$v['occupied_we'];
           $data[$v['id']]['text'] = $v['name'].'-'.$v['coilno'].'-'.$st.'-'.$v['d'].'*'.$v['w'].'*'.$v['h'].'-'.$da.'吨';
           $data[$v['id']]['d'] = $v['d'];
           $data[$v['id']]['w'] = $v['w'];
           $data[$v['id']]['po_name'] = $v['po_name'];
           $data[$v['id']]['po_price'] = $v['po_price'];
       
       }
        $this->success("成功", '', $data);
    }
    public function getplate(){
        $coilarr=Db::name('plate')->where('delete_time', 0)->whereIn('type','4,6,7,1,2')
            ->order('id desc')
            ->select();
//        var_dump(Db::name('plate')->getLastSql());
        $data = array();
        $num = 0;
        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');
        $steel_grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');
        $coil_arr_da = Db::name('coil')
            ->column('name', 'id');
        $partno=Db::name('partno')
            ->column('name,drawingno', 'partno_id');

//var_dump($partno);

        foreach ($coilarr as $k => $v) {
//            var_dump($v);
            $data[$v['id']]=$v;
            $data[$v['id']]['value'] = $v['id'];
            $ty= isset($type[$v['type']]) ? $type[$v['type']] : '';
            $st= isset($steel_grade_id_arr[$v['grade_id']]) ? $steel_grade_id_arr[$v['grade_id']] : '';
            $co= isset($coil_arr_da[$v['coil_id']]) ? $coil_arr_da[$v['coil_id']] : '';
            if(isset($v['partno_id'])) {
                $data[$v['id']]['partno_id'] = $v['partno_id'];
                $pa = isset($partno[$v['partno_id']]['name']) ? $partno[$v['partno_id']]['name'] . '-' . $partno[$v['partno_id']]['drawingno'] : '';
            }else{
                $pa='';
            }
            $da=$v['sheets'];
            $data[$v['id']]['text'] = $v['name'].'-'.$ty.'-'
            .$st.'-'.$pa.
                '-'.$co.'-'.$v['d'].'*'.$v['w'].'*'.$v['h'].'-'.$da.'张'. '-'.$v['weight'].'吨'. '-'.'采购订单号:'.$v['po_name']. '-'.'采购原料单价:'.$v['po_price'];
            $data[$v['id']]['d'] = $v['d'];
            $data[$v['id']]['w'] = $v['w'];
            $data[$v['id']]['sheets'] = $v['sheets'];
            $data[$v['id']]['weight'] = $v['weight'];
            $data[$v['id']]['h'] = $v['h'];
            $data[$v['id']]['po_name'] = $v['po_name'];
            $data[$v['id']]['po_price'] = $v['po_price'];


        }
        $this->success("成功", '', $data);
    }


    public function getunpackplate(){
        $coilarr=Db::name('plate')->where('delete_time', 0)
            ->order('id desc')
            ->select();
//        var_dump(Db::name('plate')->getLastSql());
        $data = array();
        $num = 0;
        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');
        $steel_grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');
        $coil_arr_da = Db::name('coil')
            ->column('name', 'id');
        $partno=Db::name('partno')
            ->column('name,drawingno', 'partno_id');

//var_dump($partno);

        foreach ($coilarr as $k => $v) {
//            var_dump($v);
            $data[$v['id']]=$v;
            $data[$v['id']]['value'] = $v['id'];
            $ty= isset($type[$v['type']]) ? $type[$v['type']] : '';
            $st= isset($steel_grade_id_arr[$v['grade_id']]) ? $steel_grade_id_arr[$v['grade_id']] : '';
            $co= isset($coil_arr_da[$v['coil_id']]) ? $coil_arr_da[$v['coil_id']] : '';
            if(isset($v['partno_id'])) {
                $data[$v['id']]['partno_id'] = $v['partno_id'];
                $pa = isset($partno[$v['partno_id']]['name']) ? $partno[$v['partno_id']]['name'] . '-' . $partno[$v['partno_id']]['drawingno'] : '';
            }else{
                $pa='';
            }
            $da=$v['sheets'];
            $data[$v['id']]['text'] = $v['name'].'-'.$ty.'-'
                .$st.'-'.$pa.
                '-'.$co.'-'.$v['d'].'*'.$v['w'].'*'.$v['h'].'-'.$da.'张';
            $data[$v['id']]['d'] = $v['d'];
            $data[$v['id']]['w'] = $v['w'];
            $data[$v['id']]['sheets'] = $v['sheets'];
            $data[$v['id']]['weight'] = $v['weight'];
            $data[$v['id']]['h'] = $v['h'];



        }
        $this->success("成功", '', $data);
    }
    /**获取库位类型
     * @return void
     */

    function gettype()
    {
        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');
//        $data= array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库')
        $data = array();
        $num = 0;
        foreach ($type as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $k;
            $data[$num]['text'] = $v;
            $num++;
        }

        $this->success("成功", '', $data);

    }

    /**获取自主 来料
     * @return void
     */
    function getcompanytype()
    {
        $companytypea_arr = array(1 => '自主', 2 => '来料', 3 => '委外');
        $data = array();
        $num = 0;
        foreach ($companytypea_arr as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $k;
            $data[$num]['text'] = $v;
            $num++;
        }

        $this->success("成功", '', $data);
    }

    /**
     * 获取合同信息
     */
    public function getht()
    {
        $ht_id_arr = Db::name('ht')->where('delete_time', 0)->select();
        $data = array();
        $num = 0;

       foreach ($ht_id_arr as $k => $v) {
//            var_dump($v);
           $data[$num]['value'] = $v['id'];
           $data[$num]['text'] = $v['ht_number'].'-'.$v['ht_name'];
           $num++;
       }
        $this->success("成功", '', $data);
    }

    /**获取自主 来料
     * @return void
     */
    function getctype()
    {
        $companytypea_arr = array(1 => '内贸', 2 => '调试', 3 => '委外', 4 => '外贸');
        $data = array();
        $num = 0;
        foreach ($companytypea_arr as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $k;
            $data[$num]['text'] = $v;
            $num++;
        }

        $this->success("成功", '', $data);
    }

    /**获取牌号
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getgrade_id()
    {
        $name = $this->request->param("name");
        if (trim($name)) {
            $steel_grade_id_arr = Db::name('grade')
                ->where('name', 'like', '%' . trim($name) . '%')
                ->where('delete_time', 0)->select();
        } else {
            $steel_grade_id_arr = Db::name('grade')->where('delete_time', 0)->select();
        }

       $data = array();
       $num = 0;
       foreach ($steel_grade_id_arr as $k => $v) {
//            var_dump($v);
           $data[$num]['value'] = $v['id'];
           $data[$num]['text'] = $v['g_name'];
           $num++;
       }
        $this->success("成功", '',$data);
    }

    public function getcompany()
    {
        $name = $this->request->param("name");
        if ($name) {
            $company = Db::name('company')
                ->where('name', 'like', '%' . trim($name) . '%')
                ->where('delete_time', 0)->select();
        } else {
            $company = Db::name('company')
                ->where('delete_time', 0)->select();

        }
        $data = array();
        $num = 0;

       foreach ($company as $k => $v) {
//            var_dump($v);
           $data[$num]['value'] = $v['id'];
           $data[$num]['text'] = $v['name']."(简称:".$v['abb_name'].")";
           $num++;
       }
        $this->success("成功", '',$data);
    }

    public function getware()
    {
        
        $name = $this->request->param("name");
        if ($name) {
            $company = Db::name('ware')
                ->where('warehouse_name', 'like', '%' . trim($name) . '%')
                ->where('delete_time', 0)->select();
        } else {
            $company = Db::name('ware')
                ->where('delete_time', 0)->select();

        }
        $data = array();
        $num = 0;
        foreach ($company as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $v['warehouse_id'];
            $data[$num]['text'] = $v['warehouse_name'];
            $num++;
        }
        $this->success("成功",'', $data);
    }

    public function getlist()
    {
        $user_id = $this->getUserId();

    }

    public function get_plate_one()
    {
        $id = $this->request->param("id");
        if (!$id) {
            $this->error('缺少参数');
        }
        $plate = Db::name('plate')
            ->where('id', $id)
            ->where('delete_time', 0)
            ->order('add_time desc')
            ->select();
//        var_dump(Db::name('plate')->getLastSql());

        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');

        $companytypea_arr = array(1 => '自主', 2 => '来料', 3 => '委外');

        $company_id_arr = Db::name('company')
            ->column('name', 'id');

        $ht_id_arr = Db::name('ht')
            ->column('ht_number', 'id');

        $grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');

        $steel_grade_id_arr = Db::name('steel_grade')
            ->column('name', 'id');

        $plate_id_arr = Db::name('plate')
            ->column('name', 'id');
        $coil_id_arr_da = Db::name('coil')
            ->column('coilno', 'id');
        $data = array();
        foreach ($plate as $k => $v) {
            $data[$k] = $v;
            $data[$k]['weight'] = $v['weight'] * 1000;

            $data[$k]['update_time'] = date('Y-m-d', $v["update_time"]);

//类型
            if ($v["processes"] == null) {
                $data[$k]['processes'] = '';
            }
            if ($v["ware_id"] == null) {
                $data[$k]['ware_id'] = '';
            }
            $data[$k]['grade_id'] = isset($grade_id_arr[$v['grade_id']]) ? $grade_id_arr[$v['grade_id']] : '';
            $data[$k]['steel_id'] = isset($steel_grade_id_arr[$v['steel_id']]) ? $steel_grade_id_arr[$v['steel_id']] : '';
            $data[$k]['coil_id'] = isset($coil_id_arr_da[$v['coil_id']]) ? $coil_id_arr_da[$v['coil_id']] : '';
            $data[$k]['ht_id'] = isset($ht_id_arr[$v['ht_id']]) ? $ht_id_arr[$v['ht_id']] : '';
            $data[$k]['company_id'] = isset($company_id_arr[$v['company_id']]) ? $company_id_arr[$v['company_id']] : '华星科技';
            $data[$k]['parent_id'] = isset($plate_id_arr[$v['parent_id']]) ? $plate_id_arr[$v['parent_id']] : '';
            $data[$k]['admin_id'] = isset($user_arr[$v['admin_id']]) ? $user_arr[$v['admin_id']] : '';
            $data[$k]['companytype'] = isset($companytypea_arr[$v['companytype']]) ? $companytypea_arr[$v['companytype']] : '';

            if ($v['type'] != 1 && $v['type'] != 6) {
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';
            } else {

//                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] .  $data[$k]['grade_id'] : '';
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';

            }
            $data[$k]['d'] = floatval($v['d']);


        }
        $this->success("成功", $data);
    }

    public function get_plate()
    {
        $plate = Db::name('plate')
            ->where('delete_time', 0)
            ->order('id desc')
            ->limit(1, 100)->select();

        $type = array(1 => '成品库', 2 => '实验库', 3 => '废料库', 4 => '半成品库', 5 => '头尾库', 6 => '封闭库', 7 => '原料库', 8 => '委外库');

        $companytypea_arr = array(1 => '自主', 2 => '来料', 3 => '委外');

        $company_id_arr = Db::name('company')
            ->column('name', 'id');

        $ht_id_arr = Db::name('ht')
            ->column('ht_number', 'id');

        $grade_id_arr = Db::name('grade')
            ->column('g_name', 'id');

        $steel_grade_id_arr = Db::name('steel_grade')
            ->column('name', 'id');

        $plate_id_arr = Db::name('plate')
            ->column('name', 'id');
        $coil_id_arr_da = Db::name('coil')
            ->column('coilno', 'id');
        $data = array();
        foreach ($plate as $k => $v) {
            $data[$k] = $v;
            $data[$k]['weight'] = $v['weight'] * 1000;

            $data[$k]['update_time'] = date('Y-m-d', $v["update_time"]);

//类型
            if ($v["processes"] == null) {
                $data[$k]['processes'] = '';
            }
            if ($v["ware_id"] == null) {
                $data[$k]['ware_id'] = '';
            }

            $data[$k]['grade_id'] = isset($grade_id_arr[$v['grade_id']]) ? $grade_id_arr[$v['grade_id']] : '';
            $data[$k]['steel_id'] = isset($steel_grade_id_arr[$v['steel_id']]) ? $steel_grade_id_arr[$v['steel_id']] : '';
            $data[$k]['coil_id'] = isset($coil_id_arr_da[$v['coil_id']]) ? $coil_id_arr_da[$v['coil_id']] : '';
            $data[$k]['ht_id'] = isset($ht_id_arr[$v['ht_id']]) ? $ht_id_arr[$v['ht_id']] : '';
            $data[$k]['company_id'] = isset($company_id_arr[$v['company_id']]) ? $company_id_arr[$v['company_id']] : '华星科技';
            $data[$k]['parent_id'] = isset($plate_id_arr[$v['parent_id']]) ? $plate_id_arr[$v['parent_id']] : '';
            $data[$k]['admin_id'] = isset($user_arr[$v['admin_id']]) ? $user_arr[$v['admin_id']] : '';
            $data[$k]['companytype'] = isset($companytypea_arr[$v['companytype']]) ? $companytypea_arr[$v['companytype']] : '';

            if ($v['type'] != 1 && $v['type'] != 6) {
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';
            } else {

//                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] .  $data[$k]['grade_id'] : '';
                $data[$k]['type'] = isset($type[$v['type']]) ? $type[$v['type']] : '';

            }
            $data[$k]['d'] = floatval($v['d']);


        }
        $this->success("成功", $data);
    }

    public function getdd()
    {
        $this->success('', 'command.setSize(70, 52)
				command.setGap(0)
				command.setCls()
				// command.setText(0, 30, "TSS24.BF2", 1, 1, "图片")
				command.setQR(40, 260, "L", 5, "A", item.name)
				// command.setText(60, 90, "TSS24.BF2", 1, 1, "佳博智汇")
				command.setText(100, 20, "TSS24.BF2", 1, 1, "山东华星新材料科技有限公司")
				command.setText(20, 50, "TSS24.BF2", 1, 1, " Shandong Huaxing New Material Tech Co., Ltd")
				command.setText(170, 100, "TSS24.BF2", 1, 1, "捆包号："+item.name)
				command.setText(170, 140, "TSS24.BF2", 1, 1, "牌号："+item.grade_id)
				command.setText(170, 180, "TSS24.BF2", 1, 1, "库位："+item.kuwei)
				command.setText(170, 220, "TSS24.BF2", 1, 1, "规格："+item.d+' * '+item.w+' * '+item.h)
				command.setText(170, 260, "TSS24.BF2", 1, 1, "张数："+item.sheets+"   理重(KG)："+item.weight)
				command.setText(170, 300, "TSS24.BF2", 1, 1, "库区："+item.name)
				command.setText(170, 340, "TSS24.BF2", 1, 1, "来料："+item.companytype)
				command.setText(170, 380, "TSS24.BF2", 1, 1, "所属公司："+item.company_id)');
    }

    /**物料添加
     * @return void
     */
    public function addplate()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));

        $params['update_time'] = time();
        $params['add_time'] = time();
        $params['admin_id'] = $this->getUserId();


        $id = Db::name('plate')->insertGetId($params);
        $params['id'] = $id;
        cmf_install_log($params, 'plate_log', 'add');
        if ($id) {
//            $id = input('get.id/d');

            if (!(is_numeric($id) && $id > 0)) {
                $this->error('非法参数');
            }
            $param = Db::name('Plate')->where('id', $id)->find();
            $name = getname($param['type']);
//        var_dump($name);
            Db::name('Plate')->where('id', $id)->update(array('name' => $name));
            $params = Db::name('Plate')->where('id', $id)->find();
            cmf_install_log($params, 'plate_log', 'update');

            $this->success("成功", $id);
        } else {
            $this->error('新增失败');
        }
//        $this->success('操作成功', 'index');
    }

    function update()
    {
        $params = array_map(function ($v) {
            return is_array($v) ? implode(',', $v) : $v;
        }, input('post.'));
        $data = array("appid" => "__UNI__360E35E",
            "version" => "1.0.3",
            "name" => "华星科技",
            "imei" => "",
            "os" => "Android",
            "wgtUrl" => cmf_get_file_download_url("admin/20240322/81c0cee074084d71a9df22096bd25626.apk")
        );
        if ($params['appid'] == $data['appid'] && $params['version'] == $data['version']) {
            $this->error('无更新');
        } else {
            $this->success('成功', $data);
        }

    }
    public  function gettp(){
        $type_id_arr = Db::name('pp')->where('id','>',1)
            ->where('id','<',11)
            ->field('id,name')->select();
        $data = array();
        $num = 0;
        foreach ($type_id_arr as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $v['id'];
            $data[$num]['text'] = $v['name'];
            $num++;
        }
        $this->success("成功",'', $data);
    }
    public  function gettphj(){
        $type_id_arr = Db::name('pp')->where('id',11)
            ->field('id,name')->select();
        $data = array();
        $num = 0;
        foreach ($type_id_arr as $k => $v) {
//            var_dump($v);
            $data[$num]['value'] = $v['id'];
            $data[$num]['text'] = $v['name'];
            $num++;
        }
        $this->success("成功",'', $data);
    }

}
